package com.hm.manage.pach;

import com.hm.manage.domain.Shuj;
import com.hm.manage.service.IShujService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Slf4j
@Component
public class GetExample {

    String Cookie = "JSESSIONID=A7F632DE13313DDF7F3DC4A4A0F1C3DB";
    String token = "db92ca4dbb6a41dca35c5c888e5d2745";

    @Autowired
    private IShujService shujService;

    /***
     * http://psvs.szpt.ga/api/systemArchive/archiveDetail/320412010001202504291623100000001
     * 获取系统备案详情
     * @return
     */
    public String getCtbaxq(String archiveCode) {
        if (archiveCode == null || "".equals(archiveCode)) {
            return "";
        }
        try {
            // 0、先获取鉴权信息
            Shuj shuj = shujService.getById(1);
            Cookie = shuj.getCcookie();
            token = shuj.getTtoken();

            // 1.拼接请求url
            String baseUrl = "http://psvs.szpt.ga/api/systemArchive/archiveDetail/";
            String apiUrl = baseUrl+archiveCode;
            URL url = new URL(apiUrl);

            // 2. 打开连接并设置基础属性
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10000); // 连接超时 5 秒
            conn.setReadTimeout(20000);    // 读取超时 10 秒

            // 3. 设置请求头（可选）
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Accept-Encoding","gzip, deflate");
            conn.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
            conn.setRequestProperty("Connection","keep-alive");
            conn.setRequestProperty("Cookie", Cookie);
            conn.setRequestProperty("Host","psvs.szpt.ga");
            conn.setRequestProperty("Referer","http://psvs.szpt.ga/");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token",token);

            // 4. 获取响应状态码
            int status = conn.getResponseCode();
            System.out.println("HTTP 状态码: " + status);

            // 5. 读取响应内容
            String response;
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            conn.getInputStream(),
                            StandardCharsets.UTF_8))) {
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                response = sb.toString();
            }

            //System.out.println("响应内容:\n" + response);
            return response;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * http://psvs.szpt.ga/api/org/orgDetail/320402000001202505071644580000001
     * @param archiveCode
     * @return
     */
    public String getPics(String orgCode){
        if (orgCode == null || "".equals(orgCode)) {
            return "";
        }
        try {
            // 0、先获取鉴权信息
            Shuj shuj = shujService.getById(1);
            Cookie = shuj.getCcookie();
            token = shuj.getTtoken();

            // 1.拼接请求url
            String baseUrl = "http://psvs.szpt.ga/api/org/orgDetail/";
            String apiUrl = baseUrl+orgCode;
            //log.info("请求路径：{}",apiUrl);
            URL url = new URL(apiUrl);

            // 2. 打开连接并设置基础属性
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10000); // 连接超时 5 秒
            conn.setReadTimeout(20000);    // 读取超时 10 秒

            // 3. 设置请求头（可选）
            conn.setRequestProperty("Accept", "application/json, text/plain, */*");
            conn.setRequestProperty("Accept-Encoding","gzip, deflate");
            conn.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
            conn.setRequestProperty("Connection","keep-alive");
            conn.setRequestProperty("Cookie", Cookie);
            conn.setRequestProperty("Host","psvs.szpt.ga");
            conn.setRequestProperty("Referer","http://psvs.szpt.ga/");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token",token);

            // 4. 获取响应状态码
            int status = conn.getResponseCode();
            System.out.println("HTTP 状态码: " + status);

            // 5. 读取响应内容
            String response;
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            conn.getInputStream(),
                            StandardCharsets.UTF_8))) {
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                response = sb.toString();
            }

            //System.out.println("响应内容:\n" + response);
            return response;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
